package newBee.class4;

import java.util.Arrays;

/**
 * 贪心算法
 * <p>
 * https://leetcode.cn/problems/largest-number/
 * 最大数
 */
public class Code01_largestNumber {

    public static void main(String[] args) {
        int[] arr = {9, 2, 5, 7, 1};
        String s = largestNumber(arr);
        System.out.println("s = " + s);
    }

    public static String largestNumber(int[] nums) {
        if (nums == null || nums.length == 0) {
            return "";
        }
        String[] arr = new String[nums.length];
        int i = 0;
        for (int num : nums) {
            arr[i++] = "" + num;
        }
        Arrays.sort(arr, (a, b) -> (b + a).compareTo(a + b));
        if (arr[0].equals("0")) return "0";

        StringBuilder b = new StringBuilder();
        for (String s : arr) {
            b.append(s);
        }
        return b.toString();
    }

}
